package ru.alt22.visor.php.ui.providers;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;

import ru.alt22.visor.php.ui.PluginImages;
import ru.alt22.visor.php.unittests.core.PHPTest;
import ru.alt22.visor.php.unittests.core.PHPTestCase;
import ru.alt22.visor.php.unittests.core.PHPTestSuite;

public class TestsTreeLabelProvider implements ILabelProvider, ITableLabelProvider {
	private final static Image phpProjectImage = PluginImages.INFO_PHP_PROJECT.createImage();
	private final static Image testCaseImage = PluginImages.INFO_TEST_CASE.createImage();
	private final static Image testSuiteImage = PluginImages.INFO_TEST_SUITE.createImage();

	public Image getImage(Object element) {
		return getColumnImage(element, 0);
	}

	public String getText(Object element) {
		return getColumnText(element, 0);
	}

	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	public void dispose() {
	}

	public void addListener(ILabelProviderListener listener) {
	}

	public void removeListener(ILabelProviderListener listener) {
	}

	public Image getColumnImage(Object element, int columnIndex) {
		if (0 != columnIndex) {
			return null;
		}

		if (element instanceof IProject) {
			return phpProjectImage;
		}
		if (element instanceof PHPTestSuite) {
			return testSuiteImage;
		}
		if (element instanceof PHPTestCase) {
			return testCaseImage;
		}
		return null;
	}

	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof PHPTest) {
			return getColumnTextFor((PHPTest) element, columnIndex);
		}
		if (element instanceof IProject) {
			return getColumnTextFor((IProject) element, columnIndex);
		}
		return null;
	}

	private String getColumnTextFor(IProject project, int columnIndex) {
		if (0 != columnIndex) {
			return null;
		}
		return project.getName();
	}

	private String getColumnTextFor(PHPTest element, int columnIndex) {
		PHPTest test = (PHPTest) element;

		switch (columnIndex) {
			case 0:
				return test.getName();
			case 1:
				if (!(element instanceof PHPTestCase)) {
					return null;
				}
				if (null == test.getResult()) {
					return "?";
				}
				return test.getResult().getStatusString(); 
			default:
				return null;
		}
	}

}